//
//  HomeCubeView.m
//  Rail
//
//  Created by hychen on 2018/3/1.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "HomeCubeView.h"

#define kCubeCol 3
#define kCubeHeight 90.f


@implementation HomeCubeModel

@end

@interface HomeCubeView () <UIScrollViewDelegate>

@end

@implementation HomeCubeView

+ (CGFloat)heightForCount:(NSInteger)count {
    if (count < 1) {
        return 0;
    }
    return (count/kCubeCol + 1)*kCubeHeight+10;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.container];
        [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(self);
            make.bottom.mas_equalTo(-10);
        }];
        
        UIView *line = [UIView new];
        line.backgroundColor = Color_Background;
        [self addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(10);
        }];
        
    }
    return self;
}

- (void)prepareCubeView {
    [self.container removeAllSubviews];
    CGFloat cubeW = ScreenWidth/3.0;
    for (int i = 0; i < self.modelArray.count; ++i) {
        //改变button的样式
        CGFloat cubeX = i%kCubeCol * cubeW;
        CGFloat cubeY = i/kCubeCol * kCubeHeight;
        CGRect frame = CGRectMake(cubeX, cubeY, cubeW, kCubeHeight);
        BMCubeIconButton *btn = [[BMCubeIconButton alloc]initWithFrame:frame];

        HomeCubeModel *model = [self.modelArray objectAtIndex:i];
        NSString *title = model.title;
        NSString *image = model.image;
        
        [btn setContentMode:UIViewContentModeCenter];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        [self.container addSubview:btn];
        btn.tag = i;
        [btn addTarget:self action:@selector(cubeIconClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

- (void)cubeIconClick:(UIButton *)btn {
    HomeCubeModel *model = [self.modelArray objectAtIndex:btn.tag];
    if (self.BlockClickCube) {
        self.BlockClickCube(btn.tag,model.sign);
    }
}


- (UIScrollView *)container {
    if (!_container) {
        _container = [[UIScrollView alloc] init];
        _container.delegate = self;
        _container.showsVerticalScrollIndicator = NO;
        _container.showsHorizontalScrollIndicator = NO;
    }
    return _container;
}

- (void)setModelArray:(NSArray *)modelArray {
    _modelArray = modelArray;
    [self prepareCubeView];
}

@end


@implementation BMCubeIconButton
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [self setTitleColor:Color_RGB(86, 86, 86) forState:UIControlStateNormal];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    CGFloat titleH = 14;
    CGFloat titleY = contentRect.size.height - titleH - 10;
    return CGRectMake(0, titleY, contentRect.size.width, titleH);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    CGFloat imageW = 45;
    CGFloat imageH = 45;
    CGFloat imageX = (contentRect.size.width - imageW) * 0.5;
    return CGRectMake(imageX, 12, imageW, imageH);
}
@end
